<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Try - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The Try statement guards one or more code lines against runtime errors and exceptions thrown by the Throw statement." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Try <span class="ver">[v1.1.04+]</span></h1>

<p>Guards one or more statements (commands or expressions) against runtime errors and exceptions thrown by the <a href="Throw.htm">throw</a> command.</p>

<pre class="Syntax"><span class="func">Try</span> <i>Statement</i></pre>
<pre class="Syntax" style="line-height: 100%">
<span class="func">Try</span>
{
    <i>Statements</i>
}
</pre>
<h2>Remarks</h2>
<p>The <em>try</em> command is usually followed by a <a href="Block.htm">block</a> - one or more statements (commands or expressions) enclosed in braces. If only a single statement is to be executed, it can be placed on the same line as <em>try</em> or on the next line, and the braces can be omitted. To specify code that executes only when <em>try</em> catches an error, use the <a href="Catch.htm">catch</a> command.</p>
<p>An exception can be thrown by the <a href="Throw.htm">throw</a> command or by the program when a runtime error occurs. When an exception is thrown from within a try block or a function called by one, the following occurs:</p>
<ul>
  <li>If there is a corresponding <a href="Catch.htm">catch</a> statement, execution continues there.</li>
  <li>If there is no catch statement but there is a <a href="Finally.htm">finally</a> statement, it is executed, but once it finishes the exception is automatically thrown again.</li>
  <li>If there is neither a catch statement nor a finally statement, execution continues at the next line outside the try block.</li>
</ul>
<p>If an exception is thrown while no try blocks are executing, an error message is shown and the current thread exits.</p>
<p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used with the <em>try</em> command. For example:</p>
<pre>try {
    ...
} catch e {
    ...
}</pre>

<h2>Related</h2>
<p><a href="Catch.htm">Catch</a>, <a href="Throw.htm">Throw</a>, <a href="Finally.htm">Finally</a>, <a href="Block.htm">Blocks</a>, <a href="OnError.htm">OnError()</a></p>

<h2 id="Examples">Examples</h2>
<div class="ex" id="ex_basic">
<p><a href="#ex_basic">#1</a>: The basic concept of try/catch/throw.</p>
<pre>try  <em>; Attempts to execute code.</em>
{
    HelloWorld()
    MakeToast()
}
<a href="Catch.htm">catch</a> e  <em>; Handles the first error/exception raised by the block above.</em>
{
    MsgBox, An exception was thrown!`nSpecifically: %e%
    <a href="Exit.htm">Exit</a>
}

HelloWorld()  <em>; Always succeeds.</em>
{
    MsgBox, Hello, world!
}

MakeToast()  <em>; Always fails.</em>
{
    <em>; Jump immediately to the try block's error handler:</em>
    <a href="Throw.htm">throw</a> A_ThisFunc &quot; is not implemented, sorry&quot;
}
</pre>
</div>

<div class="ex" id="ex_el">
<p><a href="#ex_el">#2</a>: Using try/catch instead of ErrorLevel.</p>
<pre>try
{
    <em>; The following tries to back up certain types of files:</em>
    FileCopy, %A_MyDocuments%\*.txt, D:\Backup\Text documents
    FileCopy, %A_MyDocuments%\*.doc, D:\Backup\Text documents
    FileCopy, %A_MyDocuments%\*.jpg, D:\Backup\Photos
}
catch
{
    MsgBox, 16,, There was a problem while backing the files up!
    ExitApp
}
</pre>
</div>

<div class="ex" id="ex_com">
<p><a href="#ex_com">#3</a>: Dealing with COM errors.</p>
<pre>try
{
    obj := <a href="ComObjCreate.htm">ComObjCreate</a>(&quot;<a href="http://msdn.microsoft.com/en-us/library/aa227633(v=vs.60).aspx">ScriptControl</a>&quot;)
    obj.ExecuteStatement(&quot;MsgBox &quot;&quot;This is embedded VBScript&quot;&quot;&quot;)
    obj.InvalidMethod() <em>; This line produces a runtime error.</em>
}
catch e
{
    <em>; For more detail about the object that e contains, see <a href="Throw.htm#Exception">Exception()</a>.</em>
    MsgBox, 16,, % &quot;Exception thrown!`n`nwhat: &quot; e.what &quot;`nfile: &quot; e.file
        . &quot;`nline: &quot; e.line &quot;`nmessage: &quot; e.message &quot;`nextra: &quot; e.extra
}
</pre>
</div>

<div class="ex" id="ex_nesting">
<p><a href="#ex_nesting">#4</a>: Nesting try-catch statements</p>
<pre>try Example1() <em>; Any single statement can be on the same line with a Try command.</em>
catch e
    MsgBox, Example1() threw %e%.

Example1()
{
    try Example2()
    catch e
    {
        if (e = 1)
            throw e <em>; Rethrow the exception so that the caller can catch it.</em>
        else
            MsgBox, Example2() threw %e%.
    }
}

Example2()
{
    Random, o, 1, 2
    throw o
}</pre>
</div>

</body>
</html>
